{% extends "helios/templates/cryptobase.html" %}

{% block title %}Questions for {{election.name}}{% endblock %}
{% block content %}
  <h3 class="title">{{election.name}} &mdash; Questions <span style="font-size:0.7em;">[<a href="{% url "election@view" election.uuid %}">back to election</a>]</span></h3>

  <script language="javascript">
{% if election.questions %}
  QUESTIONS = JSON.parse("{{questions_json|escapejs}}");
{% else %}
  QUESTIONS = [];
{% endif %}
{% if admin_p and not election.frozen_at %}
  ADMIN_P = true;
{% else %}
  ADMIN_P = false;
{% endif %}

  CSRF_TOKEN = '{{csrf_token}}';

  function refresh() {
      $('#questions').processTemplate({'questions' : QUESTIONS, 'admin_p' : ADMIN_P}); 

      add_answers($('#answer_table'),DEFAULT_NUM_ANSWERS);
 }
  
  function save_questions(callback) {
      $.post('./save_questions', {'questions_json' : $.toJSON(QUESTIONS), 'csrf_token': CSRF_TOKEN}, function(result) {
          if (result == "FAILURE") {
            alert("The questions could not be saved. Please check that the URLs you entered are either http:// or https://");
          } else {
            callback();
          }
      });      
  }
  
  function get_question_from_form(form) {
      var max = null;
      if (form.max.value != "")
        max = parseInt(form.max.value);
        
      var new_q = {'question' : form.question.value, 'min': parseInt(form.min.value), 'max': max, 'short_name': form.question.value, 'answers': [], 'answer_urls': [], 'choice_type': form.choice_type.value, 'tally_type': 'homomorphic', 'result_type': form.result_type.value, 'randomize_answer_order': form.randomize_answer_order.value == "1"};
      var i = 0;
      while(true) {
          i += 1;
          if (!form['answer_' + i] || !form['answer_' + i].value)
            break;
          new_q.answers[i-1] = form['answer_' + i].value;
          new_q.answer_urls[i-1] = form['answer_url_' +i].value;
      }
      
      if (new_q.answers.length == 0) {
        return null;
      }
      
      return new_q;
  }

  function question_add(form) {
      var new_q = get_question_from_form(form);
      if (new_q) {
        QUESTIONS.push(new_q);
        save_questions(refresh);
      }
  }
  
  function question_remove(q_num) {
      var new_questions = [];
      $(QUESTIONS).each(function(i, q) {
          if (i == q_num)
            return;
          new_questions.push(q);
      });
      QUESTIONS = new_questions;
      save_questions(refresh);
  }

  function question_edit(q_num) {
    $('#q_view_'+q_num).hide();
    $('#q_edit_'+q_num).show();

    // set the default values for this question
    var form = $('#question_edit_' + q_num + '_form');
    var q_data = QUESTIONS[q_num];
    form[0].min.value = q_data.min;
    form[0].max.value = q_data.max;
    form[0].question.value = q_data.question;
    form[0].result_type.value = q_data.result_type;
    form[0].randomize_answer_order.value = q_data.randomize_answer_order ? "1" : "0";

    // add the answers
    var answer_table = $('#answer_table_' + q_num);
    add_answers(answer_table, q_data.answers.length);
    $(q_data.answers).each(function(i, a) {
     form[0]['answer_' + (i+1)].value = a;
    });

    // URLs
    $(q_data.answer_urls).each(function(i, a) {
     form[0]['answer_url_' + (i+1)].value = a;
    });
}

  function question_edit_cancel(q_num) {
    $('#q_edit_'+q_num).hide();
    $('#q_view_'+q_num).show();
  }

  function question_update(q_num, form) {
      var new_q = get_question_from_form(form);
      QUESTIONS[q_num] = new_q;
      save_questions(refresh);
  }

  var DEFAULT_NUM_ANSWERS = 5;

  function add_answer(el) {
    el = $(el);
    var num_answers = el.find('input.answer').length + 1;
    el.find('tbody').append('<tr><th>&nbsp;&nbsp;&nbsp;</th><th>Answer #' + num_answers + '</th><td><input type="text" class="answer" name="answer_' + num_answers + '" size="70" /><br /><nobr>Link (optional, <tt>http</tt> or <tt>https</tt> only): <input type="url" name="answer_url_' + num_answers + '" width="50%" /></nobr></td></tr>');
  }

  function add_answers(el, num) {
    for (var i=0; i<num; i++)
      add_answer(el);
  }

  function question_move_up(num) {
    var temp_q = QUESTIONS[num];
    QUESTIONS[num] = QUESTIONS[num-1];
    QUESTIONS[num-1] = temp_q;
    save_questions(refresh);
  }

  $(document).ready(function() {
      $('#questions').setTemplateURL("/static/helios/static_templates/question.html");

      refresh();
  });
  

  </script>

  <div id="questions">
  </div>
					
			<!-- end #content -->
{% endblock %}
